﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class Insurance
    {
        public Insurance()
        { }

        public int InsuranceID
        { get; set; }

        public List<InsuranceProgram> InsurancePrograms
        { get; set; }

        public string InsuranceOrderNomer
        { get; set; }

        public double Balance
        { get; set; }

        public DateTime EffectiveDate
        { get; set; }

        public string EffectiveDateString
        {
            get { return EffectiveDate.ToShortDateString(); }
        }

        public string TerminationDateString
        {
            get { return TerminationDate.ToShortDateString(); }
        } 

        public DateTime TerminationDate
        { get; set; }

        public List<Patient> Patient
        { get; set; }

        public Owner Owner
        { get; set; }

        public string OwnerName
        {
            get { return Owner.GetFullName(); }
        }

        public Company InsurerCompany
        { get; set; }

        public string InsurerCompanyName
        {
            get { return InsurerCompany.CompanyName; }
        }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateInsurance");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@owner_id", SqlDbType.Int).Value = Owner.GetOwnerID();
            command.Parameters.Add("@owner_type_id", SqlDbType.TinyInt).Value = Owner.GetOwnerType();
            command.Parameters.Add("@nomer", SqlDbType.VarChar, 30).Value = InsuranceOrderNomer;
            command.Parameters.Add("@effective_date", SqlDbType.DateTime).Value = EffectiveDate;
            command.Parameters.Add("@temination_date", SqlDbType.DateTime).Value = TerminationDate;
            command.Parameters.Add("@balance", SqlDbType.Money).Value = Balance;
            command.Parameters.Add("@ins_company_id", SqlDbType.Int).Value = InsurerCompany.CompanyID;
            command.Parameters.Add("@insurance_id", SqlDbType.Int).Direction = ParameterDirection.Output;

            SQLDBLayer.ExecSPNonQuery(command);
            this.InsuranceID = (int)command.Parameters["@insurance_id"].Value;

            command = new SqlCommand("AddInsProgramToInsurance");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@insurance_id", SqlDbType.Int);
            command.Parameters.Add("@ins_program_id", SqlDbType.Int);

            foreach (InsuranceProgram program in InsurancePrograms)
            {
                command.Parameters["@insurance_id"].Value = this.InsuranceID;
                command.Parameters["@ins_program_id"].Value = program.InsuranceProgramID;
                SQLDBLayer.ExecSPNonQuery(command);
            }
        }
    }
}
